// DriveNotes. Copyright (c) 2011, Andrei Senchuk. All rights reserved.
package net.taviscaron.drivenotes.server.model.validators;

import net.taviscaron.drivenotes.server.model.dao.ServiceDAO;
import net.taviscaron.drivenotes.shared.exceptions.InvalidEntityException;
import net.taviscaron.drivenotes.shared.model.entity.Service;
import org.springframework.beans.factory.annotation.Required;
import static net.taviscaron.drivenotes.server.util.ExceptionUtils.invalidEntityException;

/**
 * Service validator
 * @author Andrei Senchuk
 */
public class ServiceValidator implements Validator<Service> {
    public enum Error {
        PROBLEMS_IS_EMPTY, SPARES_IS_EMPTY,
        INVALID_SERVICE_COST, INVALID_SPARES_COST,
        INVALID_ODOMETR_VALUE
    }

    private ServiceDAO serviceDAO;

    @Required
    public void setServiceDAO(ServiceDAO serviceDAO) {
        this.serviceDAO = serviceDAO;
    }

    public void validate(Service service) throws InvalidEntityException {
        if(service.getProblems() == null || service.getProblems().trim().isEmpty()) {
            throw invalidEntityException(Error.PROBLEMS_IS_EMPTY);
        }

        if(service.getSpares() == null || service.getSpares().trim().isEmpty()) {
            throw invalidEntityException(Error.SPARES_IS_EMPTY);
        }

        if(service.getServiceCost() != null && service.getServiceCost() < 0) {
            throw invalidEntityException(Error.INVALID_SERVICE_COST);
        }

        if(service.getSparesCost() == null || service.getSparesCost() < 0) {
            throw invalidEntityException(Error.INVALID_SPARES_COST);
        }

        if(service.getOdometrValue() == null || service.getOdometrValue() < 0) {
            throw invalidEntityException(Error.INVALID_ODOMETR_VALUE);
        }
    }
}
